package com.richfit.barcodesystem_cczt.activitis.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.richfit.barcodesystem_cczt.activitis.model.base.BaseBO;
import com.richfit.barcodesystem_cczt.utils.PermissionsUtils.PermissionsList;
import com.richfit.barcodesystem_cczt.utils.PermissionsUtils.PermissionsUtils;
import com.richfit.barcodesystem_cczt.utils.sharedPreferences.DatabaseHelper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseActivity extends AppCompatActivity {
    public BaseBO bo;
    public Gson gson = new Gson();
    //创建监听权限的接口对象
    PermissionsUtils.IPermissionsResult permissionsResult = new PermissionsUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
//            Toast.makeText(getParent(), "权限通过，可以做其他事情!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void forbitPermissons() {
//            finish();
//            Toast.makeText(getParent(), "权限不通过!", Toast.LENGTH_SHORT).show();
        }
    };
    //退出时的时间
    private long mExitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        bo = DatabaseHelper.getBaseBO();
        //动态权限申请
        PermissionsUtils.getInstance().chekPermissions(this, PermissionsList.permissions, permissionsResult);
    }

    //沉浸式状态栏
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        immersiveStatusBar();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        immersiveStatusBar();
    }

//    //对返回键进行监听
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//
//            exit();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    public void immersiveStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                //MIUI状态栏兼容
                if ("xiaomi".equalsIgnoreCase(Build.MANUFACTURER) || "redmi".equalsIgnoreCase(Build.MANUFACTURER)) {
                    Window window = getWindow();
                    Class clazz = window.getClass();
                    try {
                        int tranceFlag = 0;
                        int darkModeFlag = 0;
                        Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                        Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_TRANSPARENT");
                        tranceFlag = field.getInt(layoutParams);
                        field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                        darkModeFlag = field.getInt(layoutParams);
                        Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); //只需要状态栏透明 extraFlagField.invoke(window,
                        // tranceFlag, tranceFlag); 或 //状态栏透明且黑色字体 extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);
                        // 清除黑色字体 extraFlagField.invoke(window, 0, darkModeFlag);
                        extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);
                    } catch (NoSuchMethodException ignored) {
                    } catch (ClassNotFoundException ignored) {
                    } catch (NoSuchFieldException ignored) {
                    } catch (IllegalAccessException ignored) {
                    } catch (IllegalArgumentException ignored) {
                    } catch (InvocationTargetException ignored) {
                    }
                }
            }
        }
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 1500) {
            Toast.makeText(this, "再按一次退出当前应用程序", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    /**
     * 退出应用
     */
    public void exitApp() {
//        finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //就多一个参数this
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    /**
     * 处理扫描
     *
     * @param type
     * @param list
     */
    protected void handleBarCodeScanResult(String type, String[] list) {

    }

    //fragment切换
    protected void replace(Fragment fragment, @IdRes int layoutId, String tag) {
        getSupportFragmentManager().beginTransaction().replace(layoutId, fragment, tag).commit();
    }

    protected void replace(Fragment fragment, @IdRes int layoutId) {
        getSupportFragmentManager().beginTransaction().replace(layoutId, fragment, fragment.getClass().getSimpleName()).commit();
    }


}
